#include "main.h"


/**
  * @brief  通用定时器3初始化配置
  * @param  自动重装载值,该参数范围0x0000 到 0xFFFF
  * @param  预分频器,该参数范围0x0000 到 0xFFFF
  * @retval None
  */
void TIM3_Init(void)   
{
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);	        	//使能定时器3时钟
	TIM_InternalClockConfig(TIM3);							    	//时基单元时钟选择内部时钟(定时器复位后默认为内部时钟)
	
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	//每隔1ms产生一次更新中断
	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up; 	   //定时器定时模式:向上计数
	TIM_TimeBaseInitStruct.TIM_Period = 10 - 1;  			  		   //自动重装载值
	TIM_TimeBaseInitStruct.TIM_Prescaler = 7200 - 1;          		    //预分频器

	TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0;					//重复计数器(只存在于高级定时器中)
	TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStruct);      				//初始化定时器
	
	TIM_ClearFlag(TIM3,TIM_FLAG_Update);
	TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);   			  				//使能定时器更新中断

	NVIC_InitTypeDef  NVIC_InitStruct;

	NVIC_InitStruct.NVIC_IRQChannel = TIM3_IRQn;            //开启定时器中断源

	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;  //抢占优先级
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;         //响应优先级
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;            //使能定时器中断	
	NVIC_Init(&NVIC_InitStruct);    								  //初始化定时器
	
//	TIM_Cmd(TIM3,ENABLE);                     		    				//使能定时器
	
}

void TIM2_Init(void)   
{
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);	        	//使能定时器3时钟
	TIM_InternalClockConfig(TIM2);							    	//时基单元时钟选择内部时钟(定时器复位后默认为内部时钟)
	
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	//每隔1ms产生一次更新中断 
	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up; 	   //定时器定时模式:向上计数
	TIM_TimeBaseInitStruct.TIM_Period = 10 - 1;  			  		   //自动重装载值
	TIM_TimeBaseInitStruct.TIM_Prescaler = 7200 - 1;          		    //预分频器
	TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0;					//重复计数器(只存在于高级定时器中)
	TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStruct);      				//初始化定时器
	
	TIM_ClearFlag(TIM2,TIM_FLAG_Update);
	TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);   			  				//使能定时器更新中断

	NVIC_InitTypeDef  NVIC_InitStruct;

	NVIC_InitStruct.NVIC_IRQChannel = TIM2_IRQn;            //开启定时器中断源
	//LIN定时发送优先级要最低
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 2;  //抢占优先级
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 2;         //响应优先级
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;            //使能定时器中断	
	NVIC_Init(&NVIC_InitStruct);    								  //初始化定时器

	TIM_Cmd(TIM2,ENABLE);                     		    				//使能定时器	
}



void TIM4_Init(void)   
{
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);	        	//使能定时器3时钟
	TIM_InternalClockConfig(TIM4);							    	//时基单元时钟选择内部时钟(定时器复位后默认为内部时钟)
	
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;

	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up; 	   //定时器定时模式:向上计数
    //两秒产生更新中断
	TIM_TimeBaseInitStruct.TIM_Period = 50000 - 1;  			  		//自动重装载值
	TIM_TimeBaseInitStruct.TIM_Prescaler = 7200 - 1;          		    //预分频器

	TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0;					//重复计数器(只存在于高级定时器中)
	TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStruct);      				//初始化定时器
	
	TIM_ClearFlag(TIM4,TIM_FLAG_Update);
	TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);   			  				//使能定时器更新中断

	NVIC_InitTypeDef  NVIC_InitStruct;

	NVIC_InitStruct.NVIC_IRQChannel = TIM4_IRQn;            //开启定时器中断源

	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 3;  //抢占优先级
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 3;         //响应优先级
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;            //使能定时器中断	
	NVIC_Init(&NVIC_InitStruct);    						//初始化定时器	
	
//	TIM_Cmd(TIM4,ENABLE);                     		    	//使能定时器(由CAN启动)	
}


/*
void TIM2_IRQHandler(void)
{
	if (TIM_GetITStatus(TIM2,TIM_IT_Update) == SET)  	//定时器更新中断
	{
		TIM_ClearITPendingBit(TIM2,TIM_IT_Update);  			//清除更新中断标志位			
	}		
}	
*/




